package payrechargelogic

import (
	"context"
	model "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/recharge"
	schema0 "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema"
	schema "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/recharge"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/pkg/xerrors"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RechargeVipLevelUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRechargeVipLevelUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RechargeVipLevelUpdateLogic {
	return &RechargeVipLevelUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RechargeVipLevelUpdateLogic) RechargeVipLevelUpdate(in *rpc.RechargeVipLevelUpdateRequest) (*rpc.CommonResponse, error) {
	var (
		commonResp rpc.CommonResp
		dataUpdate []schema.RechargeVip
		// dataInsert []schema.RechargeVip
	)

	if len(in.Records) == 0 {
		commonResp.Code = xerrors.ParameterError
		commonResp.Message = "Not enough parameter"
		return &rpc.CommonResponse{CommonResp: &commonResp}, nil
	}
	for _, record := range in.Records {
		dataUpdate = append(dataUpdate, schema.RechargeVip{
			Model: schema0.Model{
				ID: record.Id,
			},
			Level:          record.Level,
			RechargeTimes:  record.Times,
			RechargeAmount: record.Amount,
		})
	}

	VipModel := model.NewRechargeVipModel(l.ctx, l.svcCtx.Db)

	// for _, record := range in.Records {
	//	if record.Id > 0 {
	//		dataUpdate = append(dataUpdate, schema.RechargeVip{
	//			Model: schema0.Model{
	//				ID: record.Id,
	//			},
	//			Level:          record.Level,
	//			RechargeTimes:  record.Times,
	//			RechargeAmount: record.Amount,
	//		})
	//	} else {
	//		dataInsert = append(dataInsert, schema.RechargeVip{
	//			Level:          record.Level,
	//			RechargeTimes:  record.Times,
	//			RechargeAmount: record.Amount,
	//		})
	//	}
	// }
	// VipModel := model.NewRechargeVipModel(l.ctx, l.svcCtx.Db)
	// if len(dataInsert) > 0 {
	//	err := VipModel.InsertRechargeVipBatch(&dataInsert)
	//	if err != nil {
	//		commonResp.Code = xerrors.DBError
	//		commonResp.Message = "Database operation error"
	//		return &rpc.CommonResponse{CommonResp: &commonResp}, err
	//	}
	// }

	if len(dataUpdate) > 0 {
		err := VipModel.UpdateRechargeVipBatch(dataUpdate)
		if err != nil {
			commonResp.Code = xerrors.DBError
			commonResp.Message = "Database operation error"
			return &rpc.CommonResponse{CommonResp: &commonResp}, err
		}
	}

	commonResp.Message = "success"
	return &rpc.CommonResponse{CommonResp: &commonResp}, nil
}
